<?php

namespace backend\models\searchs;

use backend\models\Category;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Article as ArticleModel;

/**
 * User represents the model behind the search form about `mdm\admin\models\User`.
 */
class Article extends ArticleModel
{
    public $cate_title;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'display', 'digest',], 'integer'],
            [['articleid', 'title', 'create_time','display', 'digest', 'excerpt', 'cate_title'], 'safe'],
        ];
    }
    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = ArticleModel::find();
        $query->joinWith('category');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
            'articleid'   => $this->articleid,
            'title'       => $this->title,
            'category_id' => $this->category_id,
            'image'       => $this->image,
            'excerpt'     => $this->excerpt,
            'digest'      => $this->digest,
            'display'     => $this->display,
            'create_time' => $this->create_time,
        ]);

        $query->andFilterWhere(['like', 'display', $this->articleid])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'image', $this->title])
            ->andFilterWhere(['like', 'excerpt', $this->create_time])
            ->andFilterWhere(['like', 'digest', $this->title])
            ->andFilterWhere(['like', 'create_time', $this->create_time])
            ->andFilterWhere(['like', 'cici_category.title', $this->cate_title]);

        return $dataProvider;
    }
}
